export const UPDATE_IS_LOADING = 'UPDATE_IS_LOADING'
export const UPDATE_HAS_MORE = 'UPDATE_HAS_MORE'
export const ADD_VALIDATE_ERROR = 'ADD_VALIDATE_ERROR'
export const RESET_VALIDATE_ERROR = 'RESET_VALIDATE_ERROR'
export const UI_SET_CURRENT_TAB = 'UI_SET_CURRENT_TAB'
export const UI_SET_PAGE = 'UI_SET_PAGE'
export const UI_SET_PAGE_INFO = 'UI_SET_PAGE_INFO'
export const UI_SET_EX = 'UI_SET_EX'
export const UI_SET_DESTROY_MEDIA = 'UI_SET_DESTROY_MEDIA'
export const UI_UPDATE_PAGE_INDEX = 'UI_UPDATE_PAGE_INDEX'
export const UI_SET_IS_SHOW_MODEL = 'UI_SET_IS_SHOW_MODEL'
export const UI_SET_TRIGGER_CLEANR_UPLOAD_QUEUE = 'UI_SET_TRIGGER_CLEANR_UPLOAD_QUEUE'
export const UI_SET_IS_MODEL = 'UI_SET_IS_MODEL'
export const UI_SET_IS_PAGE_LOADING = 'UI_SET_IS_PAGE_LOADING'
export const UI_SET_UPLOADING_MEDIA_NUM = 'UI_SET_UPLOADING_MEDIA_NUM'
export const UI_SET_TOAST_MSG = 'UI_SET_TOAST_MSG'
export const UI_SET_TOAST_SUCCESS_MSG = 'UI_SET_TOAST_SUCCESS_MSG'
export const UI_SET_TOAST_WARNING_MSG = 'UI_SET_TOAST_WARNING_MSG'

const state = {
  isLoading: false,
  isPageLoading: false,
  isShowModel: false,
  hasMore: false,
  validateErrors: [],
  currentTab: '',
  ex: '',
  destroyMedia: '',
  triggerClearUploadQueue: '',
  isModel: false,
  page: {
    totalCount: 0,
    hasMore: false,
    totalPages: 0,
    pageIndex: 0,
    pageSize: 20,
  },
  pageInfo: {
    totalCount: 0,
    hasMore: false,
    totalPages: 0,
    currentPage: 1,
    pageSize: 20,
  },
  uploadingMediaNum: 0,
  toastMsg: '',
  toastSuccessMsg: '',
  toastWarningMsg: ''
}

const getters = {
  isLoading: state => state.isLoading,
  isPageLoading: state => state.isPageLoading,
  isShowModel: state => state.isShowModel,
  ex: state => state.ex,
  destroyMedia: state => state.destroyMedia,
  triggerClearUploadQueue: state => state.triggerClearUploadQueue,
  isModel: state => state.isModel,
  currentTab: state => state.currentTab,
  page: state => state.page,
  pageInfo: state => state.pageInfo,
  toastMsg: state => state.toastMsg,
  uploadingMediaNum: state => state.uploadingMediaNum,
  toastSuccessMsg: state => state.toastSuccessMsg,
  toastWarningMsg: state => state.toastWarningMsg,
  validateErrors: state => state.validateErrors
}

const actions = {
  setIsShowModel ({ commit }, flag) {
    commit(UI_SET_IS_SHOW_MODEL, flag)
  },
  setIsPageLoading ({ commit }, flag) {
    commit(UI_SET_IS_PAGE_LOADING, flag)
  },
  setIsModel ({ commit }, flag) {
    commit(UI_SET_IS_MODEL, flag)
  },
  setPageIndex ({ commit, state }) {
    commit(UI_UPDATE_PAGE_INDEX)
  },
  updateIsLoading ({ commit, state }, flag) {
    commit(UPDATE_IS_LOADING, flag)
  },
  setEx ({ commit, state }, key) {
    commit(UI_SET_EX, key)
  },
  setDestroyMedia ({ commit, state }, key) {
    commit(UI_SET_DESTROY_MEDIA, key)
  },
  setTriggerClearUploadQueue ({ commit, state }) {
    commit(UI_SET_TRIGGER_CLEANR_UPLOAD_QUEUE)
  },
  updateHasMore ({ commit, state }, flag) {
    commit(UPDATE_HAS_MORE, flag)
  },
  addValidateError ({ commit, state }, msg) {
    commit(ADD_VALIDATE_ERROR, msg)
  },
  resetValidateError ({ commit, state }) {
    commit(RESET_VALIDATE_ERROR)
  },
  setCurrentTab ({ commit }, tab) {
    commit(UI_SET_CURRENT_TAB, tab)
  },
  setUploadingMediaNum ({ commit }, num) {
    commit(UI_SET_UPLOADING_MEDIA_NUM, num)
  },
}

const mutations = {
  UPDATE_IS_LOADING (state, flag) {
    state.isLoading = flag
  },
  UI_SET_IS_MODEL (state, flag) {
    state.isModel = flag
  },
  UI_SET_TOAST_MSG (state, msg) {
    state.toastMsg = msg
    wx.showToast({title: msg, icon: 'none'})
  },
  UI_SET_IS_PAGE_LOADING (state, flag) {
    state.isPageLoading = flag
  },
  UPDATE_HAS_MORE (state, flag) {
    state.hasMore = flag
  },
  ADD_VALIDATE_ERROR (state, msg) {
    state.validateErrors.push(msg)
  },
  RESET_VALIDATE_ERROR (state) {
    state.validateErrors = []
  },
  UI_SET_CURRENT_TAB (state, tab) {
    state.currentTab = tab
  },
  UI_SET_TOAST_SUCCESS_MSG (state, msg) {
    state.toastSuccessMsg = msg
  },
  UI_SET_TOAST_WARNING_MSG (state, msg) {
    state.toastWarningMsg = msg
  },
  UI_SET_EX (state, key) {
    state.ex = key
  },
  UI_SET_DESTROY_MEDIA (state, key) {
    state.destroyMedia = key
  },
  UI_SET_TRIGGER_CLEANR_UPLOAD_QUEUE (state) {
    state.triggerClearUploadQueue = new Date()
  },
  UI_SET_PAGE (state, page) {
    state.page = page
  },
  UI_SET_PAGE_INFO (state, page) {
    state.pageInfo = page
  },
  UI_SET_IS_SHOW_MODEL (state, flag) {
    state.isShowModel = flag
  },
  UI_UPDATE_PAGE_INDEX (state, pageIndex) {
    state.page.pageIndex = pageIndex
  },
  UI_SET_UPLOADING_MEDIA_NUM (state, num) {
    state.uploadingMediaNum = num
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}
